package come.example.domain.strategy.service.rule.tree.factory;

import come.example.domain.strategy.model.vo.RuleLogicCheckTypeVO;
import come.example.domain.strategy.model.vo.RuleTreeVO;
import come.example.domain.strategy.service.rule.tree.ILogicTreeNode;
import come.example.domain.strategy.service.rule.tree.factory.engine.IDecisionTreeEngine;
import come.example.domain.strategy.service.rule.tree.factory.engine.impl.DecisionTreeEngine;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 规则树工厂
 */

@Service
public class DefaultTreeFactory {

    // map注入
    // 通过Spring自动收集所有ILogicTreeNode接口的实现类 根据component
    private final Map<String, ILogicTreeNode> logicTreeNodeGroup;

    // 自动生成构造函数之后就可以注入（实例化）参数了
    public DefaultTreeFactory(Map<String, ILogicTreeNode> logicTreeNodeGroup) {
        this.logicTreeNodeGroup = logicTreeNodeGroup;
    }

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class TreeActionEntity {
        private RuleLogicCheckTypeVO ruleLogicCheckType;
        private StrategyAwardVO strategyAwardVO;
    }

    public IDecisionTreeEngine openLogicTree(RuleTreeVO ruleTreeVO) {
        return new DecisionTreeEngine(logicTreeNodeGroup, ruleTreeVO);
    }

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    // 在工厂里提供数据对象，聚合在这里更好管理
    public static class StrategyAwardVO {
        /** 抽奖奖品ID - 内部流转使用*/
        private Integer awardId;

        /** 抽奖奖品规则*/
        private String awardRuleValue;
    }

}
